package com.easy.venue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.ConcertBean;
import com.easy.common.bean.OrderBean;
import com.easy.common.bean.SeatBean;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IConcertService;
import com.easy.common.service.ISeatService;
import com.easy.venue.mapper.SeatMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class SeatServiceImpl extends ServiceImpl<SeatMapper, SeatBean> implements ISeatService {
    @DubboReference
    private IConcertService concertService;
    @Override
    public SeatBean getSeatById(Integer id) {
        return super.getById(id);
    }

    @Override
    public int addSeat(SeatBean seat) {

        if (super.save(seat)) {
            return 1;
        }
        return 0;
    }

    @Override
    public int updateSeat(SeatBean seat) {
        return super.updateById(seat)?1:0;
    }

    @Override
    public int deleteSeat(Integer id) {
        return super.removeById(id)?1:0;
    }
    @Override
    public List<SeatBean> getAll(SeatBean seat) {
        LambdaQueryWrapper<SeatBean> queryWrapper = new LambdaQueryWrapper<>();
        return super.list(queryWrapper);
    }

    @Override
    public IPage<SeatBean> getPage(SeatBean seat, Page<SeatBean> page) {
        LambdaQueryWrapper<SeatBean> queryWrapper = new LambdaQueryWrapper<>();
        if(seat!=null&& seat.getConcertid()!=null) {
            queryWrapper.like(SeatBean::getConcertid, seat.getConcertid());
        }
        if(seat!=null&& seat.getArea()!=null&& !seat.getArea().isEmpty()) {
            queryWrapper.like(SeatBean::getArea, seat.getArea());
        }
        return super.page(page,queryWrapper);
    }

    @Override
    public List<SeatBean> getSeatsByConcertId(Integer concertId) {
        return lambdaQuery().eq(SeatBean::getConcertid, concertId).list();
    }

//    @Override
//    public IPage<SeatBean> getConcertTitle(Integer concertId, Page<SeatBean> page) {
//        IPage<SeatBean> ipage = super.page(page,new QueryWrapper<SeatBean>());
//        ipage.getRecords().forEach(seatBean -> {
//            ConcertBean concert = concertService.getConcertById(seatBean.getConcertid());
//            seatBean.setConcertTitle(concert.getTitle());
//        });
//        return ipage;
//    }
    @Override
    public IPage<SeatBean> getConcertTitle(Page<SeatBean> page) {
        IPage<SeatBean> ipage = super.page(page,new QueryWrapper<SeatBean>());
        ipage.getRecords().forEach(seat -> {
            ConcertBean concert = concertService.getConcertById(seat.getConcertid());
            if (concert != null) {
                seat.setConcertTitle(concert.getTitle());
            } else {
                seat.setConcertTitle("未知音乐会");
            }
        });
        return ipage;
    }

}
